
from trait import trait
from weapon import weapon
from Enumerate import Enumerate

class model():

    moveTypes = Enumerate('WALKER', 'GROUND', 'HOVER', 'AIR')
    
    name = None
    chassis = None
    type = None    
    
    movePriType= None
    movePriMP = 0
    moveSecType = None
    moveSecMP = 0

    detect = 0
    sensors = 0
    autocomm = 0
    comm = 0

    atkmod = 0
    defmod = 0

    damage = 'SLHCD'
    armor = 0 
    armor_rear = 0
    actions = 1
    size = 6

    skillATK = 2
    skillDEF = 2
    skillEW = 1
    skillLD = 0
        
    def __init__(self, jsonObj):
        self.size = 0
        self.type = "gear"
        
        self.weapons_ranged = []
        self.weapons_thrown = []
        self.weapons_melee = []
        
        self.traits_move = []
        self.traits_def = []
        self.traits_ew = []
        self.traits_aux = []
        self.traits_misc = []

        for key in self.weapons_ranged:
            print "model:__init__: Found key: %s" % key.name

        self.jsonObj = jsonObj
        if "name" not in jsonObj:
            return

        attributes = ["name", "chassis", "type", 
            "movePriType", "movePriMP", "moveSecType", "moveSecMP",
            "detect", "sensors", "autocomm", "comm",
            "atkmod",  "defmod", 
            "damage", "armor", "armor_rear", "actions", "size",
            "sp_move",  "sp_def",  "sp_ew",  "sp_misc", 
            ]
        for attrib in attributes:
            self.decodeValue(attrib)
        
        self.setMovementType()
        
        if self.armor_rear == 0:
            self.armor_rear = self.armor
        
        # Resolve traits
        if "traits" in jsonObj:
            traitList = jsonObj["traits"]
            for traitS in traitList:
                traitObj = trait(traitS)
                
                if traitObj.type == traitObj.types.MOVE:
                    self.traits_move.append(traitObj)
                elif traitObj.type == traitObj.types.DEF:
                    self.traits_def.append(traitObj)
                elif traitObj.type == traitObj.types.EW:
                    self.traits_ew.append(traitObj)
                elif traitObj.type == traitObj.types.AUX:
                    self.traits_aux.append(traitObj)
                elif traitObj.type == traitObj.types.MISC:
                    self.traits_misc.append(traitObj)
        
        # Resolve weapons
        if "weapons" in jsonObj:            
            for key in jsonObj["weapons"]:                
                #print "Found weapon (%s)" % key
                dict = jsonObj["weapons"][key]
                weaponObj = weapon(key, dict)
                
                count = 1
                if dict != None and "count" in dict:
                    count = dict["count"]
                
                for i in range(count):
                    #print "model:__init__:weapon [%s] has type [%i]" % (weaponObj.name,  weaponObj.type)                
                    if weaponObj.type == weaponObj.types.THROWN:
                        self.weapons_thrown.append(weaponObj)
                    elif weaponObj.type == weaponObj.types.MELEE:
                        self.weapons_melee.append(weaponObj)
                    else:                    
                        self.weapons_ranged.append(weaponObj)

        # Add physical attacks        
        physWeap = weapon("pa",  None)
        physWeap.dam = self.size
        self.weapons_melee.append(physWeap)
    
    def setMovementType(self):
        if self.movePriType != None:
            if self.movePriType == 'W':
                self.movePriType = self.moveTypes.WALKER
                self.movePriTypeS = "Walker"
            elif self.movePriType == 'G':
                self.movePriType = self.moveTypes.GROUND
                self.movePriTypeS = "Ground"
            elif self.movePriType == 'H':
                self.movePriType = self.moveTypes.HOVER
                self.movePriTypeS = "Hover"
            elif self.movePriType == 'A':
                self.movePriType = self.moveTypes.AIR
                self.movePriTypeS = "Air"

        if self.moveSecType != None:
            if self.moveSecType == 'W':
                self.moveSecType = self.moveTypes.WALKER
                self.moveSecTypeS = "Walker"
            elif self.moveSecType == 'G':
                self.moveSecType = self.moveTypes.GROUND
                self.moveSecTypeS = "Ground"
            elif self.moveSecType == 'H':
                self.moveSecType = self.moveTypes.HOVER
                self.moveSecTypeS = "Hover"
            elif self.moveSecType == 'A':
                self.moveSecType = self.moveTypes.AIR
                self.moveSecTypeA = "Air"


    def decodeValue(self, name):
        if name in self.jsonObj:
            setattr(self, name, self.jsonObj[name])  
            #print "model:__init__:Setting [%s]=[%s]" % (name, self.jsonObj[name])
